//
//  EPMHomeViewModel.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/27.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMHomeViewModel.h"
#import "EPMStatuesViewModel.h"
#import "EPMHomeModel.h"
#import "EPMHomePictureUrlModel.h"
@implementation EPMHomeViewModel

    +(instancetype)sheardTool{
        static EPMHomeViewModel* model;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            model = [EPMHomeViewModel new];
        });
        return model;
    }
    - (instancetype)init{
        if (self = [super init])
        {
            self.dataArray = [NSMutableArray<EPMStatuesViewModel*> new];
        }
        return self;
    }
    
    
  - (void)getHomeViewDataWithPullGesture:(BOOL)isUpPulling finished:(void(^)(BOOL,NSUInteger))finished
    {
        int64_t sinceID = 0;
        int64_t maxID = 0;
        if (isUpPulling){
            maxID = self.dataArray.lastObject.homeModel.id;
            if (maxID >0){
                maxID -= 1;
            }
        }
            else{
               
                sinceID = self.dataArray.firstObject.homeModel.id;
            }
        
        ///检查缓存中的数据
        [EPMHomeDAL checkCacheSinceID:sinceID maxID:maxID finished:^(NSArray *infoArr) {
            if ( infoArr.count == 0){
                finished(NO,0);
            }
            NSMutableArray<EPMStatuesViewModel*>* tempArr =[NSMutableArray<EPMStatuesViewModel*> new];
            NSArray<EPMHomeModel*>* arr = [NSArray yy_modelArrayWithClass:[EPMHomeModel class] json:infoArr];
            for ( EPMHomeModel* model in arr){
                EPMStatuesViewModel* statues = [EPMStatuesViewModel new];
                statues.homeModel = model;
                [tempArr addObject:statues];
            }
            
            [self loadSingPicture:tempArr.copy finiehed:finished];
            if (!isUpPulling){
                [tempArr addObjectsFromArray:self.dataArray.mutableCopy];
                //                [tempArr addObject:self.dataArray.mutableCopy];
                self.dataArray = tempArr;
                
            }else{
                [self.dataArray addObjectsFromArray:tempArr];
            }

            
        }];
        
//        [[EPMNetWorkTool sheardTool] getHomeWeiBoDataMaxID:maxID sinceID:sinceID finished:^(id res, NSError *error) {
//            if (error != nil){
//                finished(NO,0);
//            }
//            
//
//        }];
        
        
        
    }

#pragma
#pragma-判断单图
- (void)loadSingPicture:(NSArray<EPMStatuesViewModel*>*)temp finiehed:(void(^)(BOOL,NSUInteger))finished
{
    dispatch_group_t group = dispatch_group_create();
    for ( EPMStatuesViewModel* model in temp){
        if (model.homeModel.pic_urls.count == 1){
            dispatch_group_enter(group);
            
        [[SDWebImageManager  sharedManager] downloadImageWithURL:[NSURL URLWithString:model.homeModel.pic_urls.lastObject.thumbnail_pic] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                   dispatch_group_leave(group);
              }];
               
                
           
        }
        if (model.homeModel.retweeted_status.pic_urls.count == 1){
            dispatch_group_enter(group);
            [[SDWebImageManager  sharedManager] downloadImageWithURL:[NSURL URLWithString:model.homeModel.retweeted_status.pic_urls.lastObject.thumbnail_pic] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                dispatch_group_leave(group);
            }];
           
        }
    } 
    dispatch_group_notify(group,dispatch_get_main_queue(), ^{
        finished(true,temp.count);
    });
}

@end
